import { NGXLogger } from 'ngx-logger';
import { Component, OnInit, ElementRef, Input } from '@angular/core';
import { VideoContainer, VideoOwner, VideoSize, TrackWrapper, Track, TrackType, VideoZonePos, VideoZoneModel, VideoZone } from '../entity/ORTC';
import { EventService, EventMessage, EventSource } from '../services/event.service';
import { RequestReplyService } from '../services/request-reply.service';
import { OrtcService } from '../services/ortc.service';
import { Utils } from '../common/Common';
import { User, UserType } from '../entity/TT';
import { VideozoneService } from 'src/app/services/videozone.service';

const TAG = 'VideozoneComponent';

@Component({
  selector: 'app-videozone',
  templateUrl: './videozone.component.html',
  styleUrls: ['./videozone.component.scss']
})
export class VideozoneComponent implements OnInit {

  @Input()
  position: VideoZonePos;

  @Input()
  model: VideoZoneModel;

  middleVideos: VideoContainer[] = [];

  vz: VideoZone;

  constructor(
    private log: NGXLogger,
    private elementRef: ElementRef,
    private eventService: EventService,
    private videozoneService: VideozoneService, ) {
      this.log.debug(TAG, ' ...');
  }

  ngOnInit() {
    this.log.debug(TAG, '.ngOnInit ...');

    const size: number = (this.model === VideoZoneModel.M_1x1 ? 1 : 20);
    let vs: VideoSize;
    switch (this.model) {
        case VideoZoneModel.M_1x1:
          vs = VideoSize.MIDDLE;
          break;
        default:
          vs = VideoSize.SMALL;
          break;
    }

    for (let i = 0; i < size; i++) {
      const vc = new VideoContainer();

      vc.owner    = VideoOwner.MATE;
      vc.ownerId  = VideoOwner.MATE + '-' + i;

      vc.size     = vs;

      this.middleVideos.push(vc);
    }

    this.vz = {position: this.position,
      model: this.model,
      videos: this.middleVideos,
      elementRef: this.elementRef
    };

    this.videozoneService.register(this.vz);

  }

  ngOnDestroy() {
    this.log.debug(TAG, '.ngOnDestroy ...');
    this.videozoneService.unregister(this.vz);

  }

}
